<?php

namespace plugin\ycMassage\app\api\controller;

use app\common\service\UploadService;
use plugin\ycMassage\app\BaseController;
use plugin\ycMassage\app\model\MassageCollectShop;
use plugin\ycMassage\app\model\MassageShop;
use plugin\ycMassage\app\model\MassageShopComment;
use plugin\ycMassage\app\model\MassageShopThumb;
use plugin\ycMassage\utils\enum\Examine;
use plugin\ycMassage\utils\enum\State;
use support\Request;

class ShopController extends BaseController
{
    protected $noNeedLogin = ["list", "details"];
    public function list(Request $request)
    {
        $limit = $request->get('limit', 20);
        $page = $request->get('page');
        $keywords = $request->get('keywords');
        $saas_appid = $request->saas_appid;
        if (!$saas_appid) {
            $saas_appid = $request->appid;
        }
        $where = [
            'saas_appid' => $saas_appid,
            'examine' => Examine::YES['value']
        ];
        if ($keywords) {
            $where[] = ['title', 'like', "%{$keywords}%"];
        }
        if ($request->city) {
            $where[] = ['city_id', '=', $request->city];
        } elseif ($request->province) {
            $where[] = ['province_id', '=', $request->province];
        }
        $order = "create_time desc";
        $model = MassageShop::alias('s')->where($where);
        if ($request->longitude && $request->latitude) {
            $order = "6371 * acos(cos(radians($request->latitude)) * cos(radians(latitude)) * cos(radians(longitude) - radians($request->longitude)) + sin(radians($request->latitude)) * sin(radians(latitude)))";
        }
        $field = [
            'id,title, logo, province, city, area, street, latitude, longitude, collect_num, comment_num, comment_grade,people_num'
        ];
        $model->field($field);
        $model->orderRaw($order);
        if ($page) {
            $listObj = $model->paginate([
                'list_rows' => $limit,
                'page' => $page
            ]);
        } else {
            $listObj = $model->limit($limit)->select();
        }
        $list = $listObj->each(function ($item) use ($request) {
            $SORT=1000;
            # 计算距离
            $item->distance = null;
            $item->distance_text = null;
            if ($request->longitude && $request->latitude && $item->latitude && $item->longitude) {
                $item->distance = MassageHaversineDistance($request->latitude, $request->longitude, $item->latitude, $item->longitude);
                if ($item->distance > 1) {
                    $item->distance_text = round($item->distance, 2) . 'km';
                } else {
                    $item->distance_text = round($item->distance * 1000) . 'm';
                }
                $SORT+=($item->distance*1000);
            }else{
                $SORT+=10000;
            }
            $item->is_collect = 0;
            if ($request->uid) {
                $item->is_collect = MassageCollectShop::where(['shop_id' => $item->id, 'uid' => $request->uid])->count();
                if($item->is_collect){
                    $SORT-=10;
                }
            }
            $SORT-=($item->collect_num);
            $SORT-=($item->people_num);
            $SORT-=($item->comment_num);
            $SORT-=($item->comment_grade*100);
            $item->sort=$SORT;
        });
        return $this->successRes($list);
    }
    public function details(Request $request)
    {
        $id = $request->get('shop_id');
        if (!$id) {
            return $this->fail('未找到门店信息');
        }
        $saas_appid = $request->saas_appid;
        if (!$saas_appid) {
            $saas_appid = $request->appid;
        }
        $where = [];
        $where['s.id'] = $id;
        $where['s.saas_appid'] = $saas_appid;
        $model = MassageShop::alias('s')->where($where);
        $model->join('massage_user u', 'u.id = s.uid', 'LEFT');
        $field = [
            's.id,s.title, s.logo, s.province, s.city, s.area, s.street,s.address,s.name,s.mobile,s.tel, s.latitude, s.longitude, s.collect_num, s.comment_num, s.comment_grade,s.people_num',
            'u.nickname,u.headimg'
        ];
        $model->field($field);
        $Find = $model->find();
        if (!$Find) {
            return $this->fail('未找到门店信息');
        }
        if ($Find->nickname) {
            $Find->nickname = base64_decode($Find->nickname);
        }
        if (strpos($Find->headimg, 'http') !== 0) {
            $Find->headimg = UploadService::url($Find->headimg);
        }
        # 计算距离
        $Find->distance = null;
        $Find->distance_text = null;
        if ($request->longitude && $request->latitude && $Find->latitude && $Find->longitude) {
            $Find->distance = MassageHaversineDistance($request->latitude, $request->longitude, $Find->latitude, $Find->longitude);
            if ($Find->distance > 1) {
                $Find->distance_text = round($Find->distance, 2) . 'km';
            } else {
                $Find->distance_text = round($Find->distance * 1000) . 'm';
            }
        }
        $Find->thumbs = [];
        $thumbs = MassageShopThumb::where(['shop_id' => $Find->id, 'saas_appid' => $saas_appid, 'examine' => Examine::YES['value']])->limit(10)->column('url');
        if (!empty($thumbs)) {
            $Find->thumbs = UploadService::url($thumbs);
        }
        $Find->is_collect = 0;
        if ($request->uid) {
            $Find->is_collect = MassageCollectShop::where(['shop_id' => $Find->id, 'uid' => $request->uid])->count();
        }

        $where = [
            'comment.saas_appid' => $saas_appid,
            'comment.shop_id' => $Find->id,
            'comment.show' => State::YES['value'],
            'comment.examine' => Examine::YES['value']
        ];
        $comment = MassageShopComment::alias('comment')->where($where)
            ->join('massage_user u', 'u.id=comment.uid')
            ->field('comment.*,u.nickname,u.headimg')
            ->order('comment.grade desc,comment.id desc')
            ->limit(5)
            ->select()->each(function ($item) use ($saas_appid) {
                if ($item->anony) {
                    $item->nickname = '匿名用户';
                    $item->headimg = UploadService::url(getHpConfig('default_avatar', $saas_appid, 'user'));
                } else {
                    $item->nickname = base64_decode($item->nickname);
                    if (strpos($item->headimg, 'http') !== 0) {
                        $item->headimg = UploadService::url($item->headimg);
                    }
                }
            });
        $Find->comments = $comment->toArray();
        $Find->comments_count=MassageShopComment::alias('comment')->where($where)->count();
        return $this->successRes($Find);
    }
}
